<?php
set_time_limit(0);

echo 'start:';
echo date("Y-m-d H:i:s");
echo PHP_EOL;

$dp = new dianping();
//$dp->city = '上海';
//$dp->rooturl = 'http://www.dianping.com/shopall/160/0';
//$dp->channelfrom = '生活服务';
//$dp->catefrom = 'http://www.dianping.com/search/category/11/80/g5191';
//$dp->listfrom = 'http://www.dianping.com/search/category/11/80/g5191';
//$dp->viewfrom = 'http://www.dianping.com/shop/1881235';
//$dp->run();

//$dp->city = '澳门';
//$dp->rooturl = 'http://www.dianping.com/shopall/342/0';
//$dp->run();

$dp->city = '上海';
$dp->rooturl = 'http://www.dianping.com/shopall/1/0';
$dp->run();


echo PHP_EOL;
echo 'stop:';
echo date("Y-m-d H:i:s");

class dianping {
    var $dbhost = 'localhost';
    var $dbname = 'dianping';
    var $dbuser = '';
    var $dbpwd = '';
    var $tbname = 'shop_tmp';
    var $cookie = '/tmp/cookiecfP1sd';
    var $ch;
    var $rooturl; 
    var $city;
    var $channelfrom = false; //key 中文名如：美食，娱乐休闲
    var $catefrom = false; //url
    var $listfrom = false; //url
    var $viewfrom = false; //url

    function run(){
        $channel = $this->cateurls($this->rooturl);
        if($this->channelfrom){
            $channel = $this->subarr($channel,$this->channelfrom,'key');
            $this->channelfrom = false;
        }
        foreach($channel as $k0=>$v0){
            $cate = $v0;
            if($this->catefrom){
                $cate = $this->subarr($cate,$this->catefrom);
                $this->catefrom = false;
            }
            if($cate){ 
                foreach($cate as $key1=>$val1){
                    $pages = $this->listurls($val1);
                    if($this->listfrom){
                        $pages = $this->subarr($pages,$this->listfrom);
                        $this->listfrom = false;
                    }
                    if($pages){
                        foreach($pages as $val2){
                            $shops = $this->viewurls($val2);
                            if($this->viewfrom){
                                $shops = $this->subarr($shops,$this->viewfrom);
                                $this->viewfrom = false;
                            }
                            foreach($shops as $val3){
                                $r = rand(1,$this->gaptime());
                                sleep($r);
                                $shop = $this->getshop($val3);
                                $shop['shopurl'] = $val3;
                                $shop['pageurl'] = $val2;
                                $shop['cateurl'] = $val1;
                                $shop['rooturl'] = $this->rooturl;
                                $shop['city'] = $this->city;
                                $shop['channel'] = $k0;
                                $shop['category'] = $key1;
                                $shop['createtime'] = date("Y-m-d H:i:s");
                                $this->save($shop);
                                // print_r($shop);
                            }               
                        }
                    }else{
                        echo $val1.' not find page'.PHP_EOL;
                    }
                }
            }else{
                echo 'not find cate'.PHP_EOL;
            }
        }
    }

    function curl($url){
        $this->ch = curl_init();
        curl_setopt($this->ch,CURLOPT_COOKIEJAR,$this->cookie);
        curl_setopt($this->ch,CURLOPT_FOLLOWLOCATION,TRUE);
        curl_setopt($this->ch,CURLOPT_RETURNTRANSFER,TRUE);
        curl_setopt($this->ch,CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");
        curl_setopt($this->ch,CURLOPT_URL,$url);
        $html = curl_exec($this->ch);

        //$ra = rand(1,5);
        //$fp = fopen("html/h".$ra."/".time().".html","a");
        //fwrite($fp,$html);
        //fclose($fp);

        $html = str_replace("\n","",$html);
        return $html;
    }
    
    //获取分类    
    function cateurls($url){
        $html = $this->curl($url);
        preg_match('/shopallCate.+<\/h2>(.+)<\/div>/U',$html,$matches);
        //return $matches[1];
        preg_match_all('/"Bravia">(.+)<\/a>/U',$matches[1],$catename);
        //return $catename[1];
        preg_match_all('/<ul>(.+).<\/ul>/U',$matches[1],$items);
        //return $items[1];
        foreach($items[1] as $key=>$val){
            preg_match_all('/href="(.+)".+>(.+)<\/a>/U',$val,$item);
            foreach($item[1] as $k=>$v){
                $urls[$catename[1][$key]][$item[2][$k]] = 'http://www.dianping.com'.$v;
            }           
        } 
        return $urls;
    }

    //获取一个分类列表页url
    function listurls($url){
        $html = $this->curl($url);
        preg_match('/<div class="Pages">(.+)<\/div>/U',$html,$matches);
        preg_match_all('/class="PageLink".*title="(.+)"/U',$matches[1],$mat);
        $n = array_pop($mat[1]);
        for($i=1;$i<=$n;$i++){
            if($i==1) $urls[] = $url;
            else $urls[] = $url.'p'.$i;
        }
        return $urls;
    }
    
    //获取商家列表中的商家url
    function viewurls($url){
        $html = $this->curl($url);
        preg_match_all('/<li class="shopname">.+href="(.+)"/U',$html,$matches);
        foreach($matches[1] as $val){
            $urls[] = 'http://www.dianping.com'.$val;
        }
        return $urls;
    }

    function getshop($url){
        $html = $this->curl($url); 
        $shop = array();
        if(strpos($html,'shop-title')){
            preg_match('/shop-title.*>(.+)<\/h1>/U',$html,$matches);
            $shop['name'] = trim($matches[1]);        
            preg_match('/shop-intro.*<span class="J_brief-cont">(.+)<\/span>/U',$html,$matches);
            $shop['intro'] = trim($matches[1]);        
            preg_match('/street-address.*>(.+)<\/span>/U',$html,$matches);
            $shop['address'] = trim($matches[1]);        
            preg_match('/itemprop="tel".*>(.+)<\/strong>/U',$html,$matches);
            $shop['tel'] = trim($matches[1]);        
            preg_match('/class="Price">.*>(.+)<\/dd>/U',$html,$matches);
            $shop['average'] = trim($matches[1]);        
            return $shop;
        }else{
            return false;
        }
    }

    function save($arr){
        $con = mysql_connect($this->dbhost,$this->dbuser,$this->dbpwd);
        if(!$con){
            die('Could not connect: '.mysql_error());
        }
        mysql_select_db($this->dbname);
        mysql_query("SET NAMES utf8");
        foreach($arr as $key=>$val){
            $arr[$key] = mysql_real_escape_string($val);
            $s1 .= $key.",";
            $s2 .= "'".$arr[$key]."',";
        }
        $s1 = substr($s1,0,-1);
        $s2 = substr($s2,0,-1);
        $sql = "INSERT INTO ".$this->tbname." (".$s1.") VALUES (".$s2.")"; 
        //echo $sql;
        if(!mysql_query($sql)){
            die('Error: '.mysql_error());
        }
        mysql_close($con);
    }

    //数组截取函数
    function subarr($arr,$sp,$type='val'){
        $start = false;
        foreach($arr as $key=>$val){
            if($type == 'val'){
                if($val == $sp) $start = true;
                if($start) $new[$key] = $val;
            }else{
                if($key == $sp) $start = true;
                if($start) $new[$key] = $val;
            }
        }
        return $new;
    }

    function gaptime(){
        $r1 = rand(1,15);
        $arr = array(1=>3,2=>3,3=>3,4=>3,5=>30,6=>3,7=>3,8=>15,9=>3,10=>3,11=>3,12=>3,13=>3,14=>3,15=>3);
        $r2 = $arr[$r1];
        if($r2 == 30){
            return rand(31,32);
        }elseif($r2 == 15){
            return rand(10,15);
        }else{
            return rand(1,3); 
        }
    }
}
?>
